﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terraria;
using Terraria.GameContent;

namespace EAMF
{
    public partial class EAMFUtils
    {
        /*将弹幕绘制在玩家中心
         * projectile发射的弹幕，mode使用的模式，color传入的颜色，texture发射出去的贴图
         */
        public static void DrawInPlayerCenter(Projectile proj, int mode, Color lightColor, Texture2D texture = null)
        {
            if (texture is null)
                texture = TextureAssets.Projectile[proj.type].Value;

            float rotation = proj.rotation;
            float scale = proj.scale;
            int frameHeight = texture.Height / Main.projFrames[proj.type];
            int frameY = frameHeight * proj.frame;

            Rectangle rectangle = new Rectangle(0, frameY, texture.Width, frameHeight);
            Vector2 drawOrigin = proj.Size / 2;

            SpriteEffects spriteEffects = SpriteEffects.None;
            if (proj.spriteDirection == -1)
                spriteEffects = SpriteEffects.FlipHorizontally;
            switch (mode)
            {
                default:
                    return;
                case 0:
                    for (int i = proj.oldPos.Length - 1; i >= 0; i--)
                    {
                        Vector2 drawPos = proj.oldPos[i] - Main.screenPosition + drawOrigin + new Vector2(0f, proj.gfxOffY);
                        Color color = proj.GetAlpha(lightColor) * ((float)i / (float)proj.oldPos.Length);
                        Main.spriteBatch.Draw(texture, drawPos, rectangle, lightColor, rotation, drawOrigin, scale, spriteEffects, 0f);
                    }
                    return;
                case 2:
                    for (int i = proj.oldPos.Length - 1; i >= 0; i--)
                    {
                        float imagelodRot = proj.oldRot[i];
                        spriteEffects = proj.oldSpriteDirection[i] == -1 ? SpriteEffects.FlipHorizontally : SpriteEffects.None;
                        Vector2 drawPos = proj.oldPos[i] - Main.screenPosition + drawOrigin + new Vector2(0f, proj.gfxOffY);
                        Main.spriteBatch.Draw(texture, drawPos, rectangle, lightColor, imagelodRot, drawOrigin, scale, spriteEffects, 0f);
                    }
                    return;
            }
        }
    }
}
